#!/bin/bash
#
#  This script set the password for HSC-root user and the password
#  for SSL private key protection. It also generate the SSL key-pair
#  and save the public key on the floppy media.
#

#set -x

getPassword()
{
   stty -echo

   while [ $count -gt 0 ] 
   do
      echo "  Input ${UserName} password :"
      read Passwd
      echo "  Again :"
      read PasswdAgain
      if [ "$Passwd" != "$PasswdAgain" ]; then
         echo "Mis-matched password! Try it again."
         echo
         let "count = count - 1"
      elif [ ${#Passwd} -lt 6 ]; then
         echo "Password is too short! Try it again."
         echo
         let "count = count - 1"
      else
         break
      fi
   done
   PasswdAgain=""

   if [ $count -le 0 ]; then
      stty echo
      exit 1
   fi

   java -Xms20m -Xmx128m com.ibm.hsc.websm.user.SetPassword $UserName $Passwd
   if [ $? -ne 0 ]; then
      echo  "Error: Failed to save password!"
      stty  echo
      exit 2
   fi

   stty echo
}


#
# Main function start here
#
echo
echo "Final CONFIGURATION of HSC ... "
echo

JAVAPATH="/opt/IBMJava2-131/jre/bin/"
x=`type -p java 2>/dev/null`
if [ "$x" != "" ]
then
  JAVAPATH=`/usr/bin/dirname $x`
fi
export PATH=$JAVAPATH:$PATH
if [ "${DEBUG_JARS_DIRECTORY}" != "" ] ; then
  if [ -d ${DEBUG_JARS_DIRECTORY} ] ; then
    for i in ${DEBUG_JARS_DIRECTORY}/*.jar
    do
       debug_jars=${debug_jars}:$i
    done
  fi
fi

export CLASSPATH=${DEBUG_JARS_DIRECTORY}:${debug_jars}:/usr/websm/codebase/pluginjars/hsc.jar:/usr/websm/codebase/wsm.jar:$CLASSPATH

#
#  Set HSC root-user's password"
#
echo  "  1) Set HSC root-user's password"

count=3
Passwd=""
UserName="hscroot"

getPassword

setPassword $UserName $Passwd
if [ $? -ne 0 ]; then
   echo "  Error: Failed to set ${UserName}'s password."
   exit 3
fi
echo

#
# Set SSL key password
#
echo  "  2) Set SSL key password"

count=3
Passwd=""
UserName="sslkey"

#  getPassword() has already save the password in password DB.
getPassword

if [ $? -ne 0 ]; then
   echo " Error: Failed to set ${UserName} password."
   exit 4
fi
echo

#
# Generate SSL Key-pair
#
echo "  3) Generate SSL Key pairs ..."

mksslkeys $Passwd
if [ $? -ne 0 ]; then
   echo " Error: Failed to generate SSL Keys."
   exit 5
fi

Passwd="******"

echo
echo  " If you are going to copy the generated SSL certification to a floppy,"
echo  " insert the floppy to the drive and then type (Y), otherwise type (N)."
read  answer

if [ "$answer" = "Y" -o "$answer" = "y" ]; then
   (cd /opt/hsc/data; tar cvf /dev/fd0 cimom.cert)
fi

echo
echo "HSC Configuration is DONE!"
rm /.cfghsc 2> /dev/null



